package com.hilaver.dzmis.web.servlet.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.order.OrderProduct;
import com.hilaver.dzmis.order.OrderProductItem;
import com.hilaver.dzmis.product.ProductDZColor;
import com.hilaver.dzmis.service.impl.OrderProductItemServiceImpl;
import com.hilaver.dzmis.util.StringUtils;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class OrderProductItemServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	
	public final static int ACTION_GET_VARIANT = 20;
	
	public final static int ACTION_ORDER_ITEM_VALIDATE = 40;
	
	private OrderProductItem orderProductItem;
	private OrderProductItemServiceImpl orderProductItemService;

	public OrderProductItemServlet() {
		super();
		this.orderProductItemService = new OrderProductItemServiceImpl();
		this.orderProductItemService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
		this.fullName = OrderProductItem.class.getName();
		this.simpleName = OrderProductItem.class.getSimpleName();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if(this.action == ACTION_EDIT || this.action == ACTION_ORDER_ITEM_VALIDATE){
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.orderProductItemService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, this.orderProductItemService);
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			this.orderProductItemService.edit(this.orderProductItem);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.orderProductItemService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.orderProductItemService.get(this.id);
			break;
		case ACTION_GET_WITH_DESCRIPTION:
			this.setId(request);
			rtn = this.orderProductItemService.getWithDescription(this.id);
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.orderProductItemService.getAll(this.id);
			break;
		case ACTION_GET_VARIANT:
			this.setId(request);
			rtn = this.orderProductItemService.getAllVariant(this.id);
			break;
		case ACTION_ORDER_ITEM_VALIDATE:
			this.setId(request);
			this.fillObj(request);
			rtn = this.orderProductItemService.validate(this.orderProductItem);
			break;
		}
		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.orderProductItem = (OrderProductItem)this.obj;
		String orderProductId = (String)getValue(request, "orderProductId");
		if(!StringUtils.isEmpty(orderProductId)){
			OrderProduct op = new OrderProduct();
			op.setId(Integer.parseInt(orderProductId));
			this.orderProductItem.setOrderProduct(op);
		}
		
		String productDZColorId = (String)getValue(request, "productDZColorId");
		if(!StringUtils.isEmpty(productDZColorId)){
			ProductDZColor pDZColor = new ProductDZColor();
			pDZColor.setId(Integer.parseInt(productDZColorId));
			this.orderProductItem.setProductDZColor(pDZColor);
		}
		
		Object biFactoryId = super.getValue(request, "biFactoryId");
		if(biFactoryId != null && !"".equals((String)biFactoryId)){
			BiFactory factory = new BiFactory();
			factory.setId(Integer.parseInt((String)biFactoryId));
			this.orderProductItem.setBiFactory(factory);
		}
	}
	
}